<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @todo 	文章后台控制器
 * @copyright	
 * @author shiqiliang<ozil1987@gmail.com>
 * @package controllers
 * @subpackage mysite
 * @version 2013-1-10
 */
class Articles extends MY_ADMIN_Controller{
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','system','form'));
		$this->load->model('Article_model','Article');
	}
	
	function admin(){
		$cid = !empty($_REQUEST['cat_id']) ? intval($_REQUEST['cat_id']) : 0;
		unset($_REQUEST['cat_id']);
		$this->assign['rs'] = $this->_lists($cid);
		$_REQUEST['cat_id'] = $cid;
		//$this->template = "articles_admin";
		$this->display();
	}
	
	function _lists($cid = 0){
		$catType = isset( $_GET['cat_type'] ) ? $_GET['cat_type'] : null;
		$parentId = isset( $_GET['parent_id'] ) ? $_GET['parent_id'] : null;

		if($cid>0 || $catType || $parentId){
			$this->load->model('Articlecat_model','ArticleCat');
			$rs = $this->ArticleCat->doList($parentId,$catType);
			$cates = cat_options($cid,$rs);
			$cids = array();
			foreach($cates as $cate){
				$cids[] = $cate['id'];
			}
			
			if(empty($cids)) return null;
			$this->Article->db->where_in('cat_id', $cids);
		}

		$like = $this->_searchLike($this->Article);
		$where = $this->_searchWhere($this->Article);
		$map = array();
		if(!empty($like)) $map['like'] = $like;
		if(!empty($where)) $map['where'] = $where;
		//$join = array('table'=>'articlecats','on'=>'articlecats.id=articles.cat_id');
		//$rs = $this->_list($this->Article,$map,$join,'articles.*,articlecats.name',true,'update_on',false);
		
		$map['join'] = array('table'=>'articlecats','on'=>'articlecats.id=articles.cat_id');
		$map['field'] = 'articles.*,articlecats.name';
		$map['order'] = 'update_on desc';
		$rs = $this->getPager($this->Article,$map);
		//echo $this->Article->db->last_query();exit;
		return $rs;
	}
}